package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WFlightRewardsProjectDao;
import com.ccp.dev.qbdms.dao.WRewardsMoneyAllocationDao;
import com.ccp.dev.qbdms.model.WFlightRewardsProject;
import com.ccp.dev.qbdms.model.WQbdXhdh;
import com.ccp.dev.qbdms.model.WRewardsMoneyAllocation;
import com.ccp.dev.qbdms.model.vo.WFlightRewardsProjectVo;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 飞行试验奖惩项目管理 信息操作服务类
 *
 * @author hw
 * @date 2020-05-25 15:16:44
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WFlightRewardsProjectService extends BaseService<WFlightRewardsProject> {

    @Resource
    private WFlightRewardsProjectDao dao;
    @Resource
    private WRewardsMoneyAllocationDao wRewardsMoneyAllocationDao;
    @Resource
    private WRewardsMoneyAllocationService wRewardsMoneyAllocationService;
    @Resource
    private ExtendSysOrgService extendSysOrgService;
    @Resource
    private WQbdXhdhService wQbdXhdhService;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<WFlightRewardsProject> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WFlightRewardsProject data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
     * 导入新增
     * @param list  数据集
     * @throws Exception 异常
     */
    public void addImportData(List<WFlightRewardsProject> list) throws Exception{
        List<WRewardsMoneyAllocation> subData = new ArrayList<>();
        String path = BaseUtil.getCurrentTenOrgCodePath();
        Date date = new Date();
        SysUser sysUser = (SysUser)ContextUtil.getCurrentUser();
        String userId = sysUser.getUserId();
        String tenantId = ContextUtil.getCurrentUserTenantId();
        SysOrg sysOrg = (SysOrg) ContextUtil.getCurrentOrg();
        String code = sysOrg.getCode();
        String orgName = sysOrg.getOrgName();
        String fillMan = sysUser.getFullName();
        String account = sysUser.getAccount();
        list.forEach(a->{
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            a.setCreateTime(date);
            a.setCreateBy(userId);
            a.setTenantId(tenantId);
            a.setIsbz(WFlightRewardsProject.ISBZ_NO);
            a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            a.setFilldeptid(code);
            a.setFilldept(orgName);
            a.setFillmanaccount(account);
            a.setFillman(fillMan);
            if(StringUtil.isEmpty(a.getBelongdeptname())){
                a.setBelongdeptname(path);
            }
            List<WRewardsMoneyAllocation> wRewardsMoneyAllocations = a.getwRewardsMoneyAllocationList();
            if(null != wRewardsMoneyAllocations && wRewardsMoneyAllocations.size()>0){
                wRewardsMoneyAllocations.forEach(b->{
                    b.setRefId(mainId);
                    b.setId(ProjectUUIDUtils.getUUIDFor32());
                    b.setCreateTime(date);
                    b.setCreateBy(userId);
                    b.setTenantId(tenantId);
                });
                subData.addAll(wRewardsMoneyAllocations);
            }
        });
        if(!list.isEmpty()){
            dao.addList(list);
            if(!subData.isEmpty()){
                wRewardsMoneyAllocationDao.addList(subData);
            }
        }
    }
    public List<WFlightRewardsProjectVo> getFxsymcList(QueryFilter queryFilter){
      return    dao.getFxsymcList(queryFilter);
    }

    /**
     * 添加飞行试验奖惩项目信息
     * @param wFlightRewardsProject 飞行试验奖惩项目信息
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(WFlightRewardsProject wFlightRewardsProject){
        wFlightRewardsProject.setAccessory(StringEscapeUtils.unescapeHtml4(wFlightRewardsProject.getAccessory()));
        String genId = ProjectUUIDUtils.getUUIDFor32();
        wFlightRewardsProject.setId(genId);
        ZhiLiangConst.addCommonFields(wFlightRewardsProject);
        if(wFlightRewardsProject.getwRewardsMoneyAllocationList().size()>0){
            wRewardsMoneyAllocationService.addList(wFlightRewardsProject.getwRewardsMoneyAllocationList(),genId);
        }
        return super.add(wFlightRewardsProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(WFlightRewardsProject wFlightRewardsProject){
        wRewardsMoneyAllocationService.updateAndAdd(wFlightRewardsProject.getwRewardsMoneyAllocationList(),wFlightRewardsProject.getId());
        return super.update(wFlightRewardsProject);
    }
    public  int updateisBz (WFlightRewardsProject wFlightRewardsProject){
        return super.update(wFlightRewardsProject);
    }

    /**
     * 导出用查询
     * @param queryFilter 查询参数
     * @return list
     */
    public List<WFlightRewardsProject> queryAll(QueryFilter queryFilter){
        List<WFlightRewardsProject> list = dao.queryAll(queryFilter);
        List<WQbdXhdh> xhdhList = wQbdXhdhService.getAll();
        list.forEach(a->{
            if(StringUtil.isNotEmpty(a.getModelname())) {
                for(WQbdXhdh xhdh : xhdhList){
                    if (xhdh.getId().equals(a.getModelname())) {
                        a.setModelname(xhdh.getfModelname());
                        break;
                    }
                }
            }
        });
        return list;
    }

    public List<Dictionary> getOriginOrgToDicList() {
        List<SysOrg> sysOrgList = extendSysOrgService.getSubLevelOrg();
        sysOrgList = sysOrgList.stream().filter(sysOrg -> sysOrg.getIsTenant() == 1).collect(Collectors.toList());
        List<Dictionary> dicList = new ArrayList<>();
        for (SysOrg sysOrg : sysOrgList) {
            Dictionary dictionary = new Dictionary();
            dictionary.setItemName(sysOrg.getOrgName());
            dictionary.setItemValue(sysOrg.getCode());
            dicList.add(dictionary);
        }
        return dicList;
    }

    public List<WFlightRewardsProject> reportList(String id,String tableName){
        return this.dao.reportList(id,tableName);
    }

    /**
     * 根据主表id查询主表加子表数据
     *
     * @param id 主表id
     * @return
     */
    public WFlightRewardsProject getParentAndChilden(String id) {
        return dao.getParentAndChilden(id);
    }

    /**
     * 保存接口传输的数据
     *
     * @param wFlightRewardsProjectList 主表model,有子表的包含子表数据
     * @return
     */
    public int saveAll(List<WFlightRewardsProject> wFlightRewardsProjectList,List<ExtendSysFile> extendSysFileList){
        ReadAttachmentUtil.getEntityData(wFlightRewardsProjectList,extendSysFileList);
        List<WFlightRewardsProject> rewardsProjectList = new ArrayList<>();
        for(WFlightRewardsProject wFlightRewardsProject : wFlightRewardsProjectList){
            if(StringUtil.isEmpty(wFlightRewardsProject.getFilldeptHospitalId())){
                wFlightRewardsProject.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wFlightRewardsProject.getFilldeptid()));
            }else{
                wFlightRewardsProject.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wFlightRewardsProject.getFilldeptHospitalId()));
            }
            // 直接插入子表数据
            wRewardsMoneyAllocationDao.addList(wFlightRewardsProject.getwRewardsMoneyAllocationList());
            // 直接插主表数据
            rewardsProjectList.add(wFlightRewardsProject);
        }
        return super.addList(rewardsProjectList);
    }
}
